<template>
    <el-container>
      <el-header>
        <div>
          <img src="../assets/logo.png" alt />
          <span>猿教育后台管理系统</span>
        </div>
        <div>
          <img src="../assets/logo.png" class="head-logo"/>
          <span class="user">{{user.username}}</span>
          <el-button type="info" @click="logout">exit</el-button>
        </div>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse? '65px':'200px'">
          <div class="toggle-button" @click="togleCollapse()">|||</div>
          <el-menu unique-opened router :collapse="isCollapse" :default-active="activePath" index :collapse-transition="false" background-color="#333744" text-color="#fff" active-text-color="#409FFF">
            <!-- unique-opened 为true则只展示一个子菜单 -->
            <!-- collapse  是否水平折叠 -->
            <!-- collapse-transition折叠动画 -->
            <!-- router 是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转 -->
            <!-- 点击左边导航可以跳转页面，:index="'/'+ item.path + ''" -->
            <!-- default-active 当前激活菜单的 index -->
            <!-- 一级菜单  -->
            <el-submenu :index="'/' + item.path + ''" v-for="item in menuList" :key = "item.id">
              <!-- 一级菜单的模板区域 -->
              <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+ subitem.path" v-for="subitem in item.children" :key = "subitem.id" @click="saveNavState('/'+ subitem.path)">
                <!-- 导航开启路由模式：
                  将index值作为导航路由 -->
                <!-- 二级菜单的模板区域 -->
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{subitem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view/>
        </el-main>
      </el-container>
    </el-container>

</template>

<script>

import '../mock/menu'
import { mapState,mapActions } from 'vuex'
export default {
  name: 'Layout',
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      iconObj: {
        '1': 'iconfont icon-yonghuliebiao',
        '2': 'iconfont icon-quanxian',
        '3': 'iconfont icon-jiaoshi',
        '4': 'iconfont icon-xinbaniconshangchuan-',
        '5': 'iconfont icon-biaoqiankuozhan_kecheng-147',
        '6': 'iconfont icon-12',
        '7': 'iconfont icon-12',
      },
      // 默认不折叠
      isCollapse: false,
      // 被激活导航地址
      activePath: ''
    }
  },
  created(){
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  computed:{
    ...mapState(['user'])
  },
  methods:{
    ...mapActions(['getMenuListSync']),
    //登出
    logout(){
      window.localStorage.clear()
      this.$router.push('/login')
    },
    // 获取请求菜单
    // async getMenuList () {
    //   const { data: res } = await this.$axios.get('menu')
    //   if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
    //   this.menuList = res.data
    //   console.log(res)
    // },
    //mock模拟数据
    async getMenuList () {
      const { data: res } = await this.$axios.get('/menu')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      localStorage.setItem('menuList', JSON.stringify(res.data))
      this.getMenuListSync(res.data)
      this.menuList = res.data
      console.log(res)
      console.log(this.$store.state,'state');
    },

    // 菜单的折叠与展开
    togleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存连接的激活地址
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373f41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    img {
      margin-left:20px ;
      height: 35px;
    }
    span {
      margin-left: 15px;
    }
    .user{
      padding-right: 20px;
    }
    .head-logo {
      width: 30px;
      height: 30px;
    }
  }
}
.el-aside {
  background-color: #333744;

  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button {
  background-color: #4A5064;
  font-size: 10px;
  line-height: 30px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  // 鼠标放上去变成小手
  cursor: pointer;
}
</style>
